/* 
 * File:   Main.cpp
 * Author: Guilherme Monteiro
 */

#include "headers/Main.h"

Main::Main() {
}

Main::Main(const Main& orig) {
}

Main::~Main() {
}

void loadConfigurationInfo() {
    json_t *root;
    json_error_t error;
    
    File* f = new File("config.json");
    vector<string> lines = f->readLines();
    
    string text;
    vector<string>::iterator it;
    for(it = lines.begin(); it != lines.end(); it++) {
        text += *it;
    }
    
    const char* finalText = text.c_str();
    
    root = json_loads(finalText, 0, &error);
    //free(finalText);
    
    json_t* data, a, b, c;
    data = json_array_get(root, 0);
//    a = json_object_get(data, "a");
//    b = json_object_get(data, "b");
//    c = json_object_get(data, "c");
//    
//    printf("a value: %s\n", json_string_value(a));
//    printf("b value: %d\n", json_integer_value(b));
//    printf("c value: %.2f", json_real_value(c));
    
    json_decref(root);
    
    printf("ok\n");
}

int main(int argc, char** argv) {
     try {
         //loadConfigurationInfo();
         
         printf("argv[0]: %s\n", argv[0]);
         printf("argv[1]: %s\n", argv[1]);
         printf("argv[2]: %s\n", argv[2]);
         printf("argv[3]: %s\n", argv[3]);
         if(argc == 4) {
            ModelListHandler* handler = new ModelListHandler(argv[2]);
            vector<GenericModel*> models = handler->getModelList();
            vector<GenericModel*>::iterator itModels;

            for(itModels = models.begin(); itModels != models.end(); itModels++) {
                TableTranscriber* transcriber = 
                    new TableTranscriber(argv[1], *itModels, argv[3]); 
                transcriber->run();
            }
            
            cout << "====> TableTranscriber executed with success." << endl;
        }
        else {
            cout << "Invalid number of arguments. Please try again." << endl;
        }
    }
    catch (NullImageException &nie) {
        cout << nie.what() << endl;
    }
    catch (InvalidFileException &ife) {
	cout << ife.what() << endl;
    }
    catch (InvalidModelException &ime) {
 	cout << ime.what() << endl;
    }
    catch (InvalidRectException &ire) {
        cout << ire.what() << endl;
    }
    catch (InvalidRotatedTagException &irte) {
        cout << irte.what() << endl;
    }
    catch (exception &ex) {        
        cout << "Some error ocurred. Please, report the bug to guilherme.gadelha [at] ccc [dot] ufcg [dot] edu [dot] br" << endl;
    }
}

